package com.hadwinling.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hadwinling.blog.entity.Admin;
import com.hadwinling.blog.entity.Message;
import com.hadwinling.blog.service.MessageService;
import com.hadwinling.blog.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.blog.controller
 * @date 2020/9/13 13:16
 */
@Controller
public class UserMessageController {
    @Autowired
    private MessageService messageService;

    @Value("${admin.article.page.pageSize}")
    private int pageSize;
    @Value("${page.navigatePage}")
    private int navigatePage;


    /**
     * 统计一共有多少信息
     * @param request
     * @return
     */
    @PostMapping("/user/message")
    @ResponseBody
    public Msg getMessageList(HttpServletRequest request){
        Admin currUser = (Admin) request.getSession().getAttribute("currUser");
        Integer messageCount = messageService.countByAdminidAndIsreadAndState(currUser.getId(), 0, 1);
        return Msg.success().add("messageCount",messageCount);
    }

    /**
     * 显示收到的通知
     * @param pageNum
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/user/message")
    public String toMessagePage(@RequestParam(name = "pageNum" ,defaultValue = "1") int pageNum, HttpServletRequest request, Model model){
        Admin currUser = (Admin) request.getSession().getAttribute("currUser");
        PageHelper.startPage(pageNum,pageSize);
        List<Message> messageList = messageService.selectAllByAdminidAndIsreadAndStateOrderByCreatetimeDesc(currUser.getId(), 0, 1);
        PageInfo<Message> pageInfo = new PageInfo<>(messageList,navigatePage);
        //当前页码
        int currPage = pageInfo.getPageNum();
        //总记录
        Long total = pageInfo.getTotal();
        //总页数
        int totalPage = pageInfo.getPages();
        //所有页面
        int [] pages = pageInfo.getNavigatepageNums();
        //保存分页信息
        model.addAttribute("currPage",currPage);
        model.addAttribute("total",total);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pages",pages);
        model.addAttribute("messageList",messageList);
        return "UserCenter/message";
    }

    /**
     * 执行已读操作
     * @param messageid
     * @return
     */
    @PostMapping("/user/readMessage")
    @ResponseBody
    public String doReadMessage(@RequestParam(name = "messageid") int messageid){
        Message message = messageService.selectByPrimaryKey(messageid);
        message.setIsread(1);
        messageService.updateByPrimaryKey(message);
        return "1";
    }

}
